package imp;

import dao.GoodsDao;
import model.Goods;
import util.DUBtil;

import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Goods_imp implements GoodsDao {

    @Override
    public boolean InsertGoods(Goods goods) {
        Connection con = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        String sql = "insert into goods(id,userid,name,imgurl" +
                ",date,price,content,state,category) values(?,?,?,?,?,?,?,?,?)";
        try {
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, goods.getId());
            pstmt.setString(2, goods.getUserId());
            pstmt.setString(3, goods.getName());
            pstmt.setString(4, goods.getImgUrl());
            pstmt.setString(5, goods.getDate());
            pstmt.setString(6, goods.getPrice());
            pstmt.setString(7, goods.getContent());
            pstmt.setString(8, goods.getState());
            pstmt.setString(9, goods.getCategory());
            pstmt.executeUpdate();
            con.close();
            pstmt.close();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean DeleteGoods(String goodsid) {
        Connection connection = new DUBtil().getConnection();
        PreparedStatement pstml = null;
        String sql = "delete from goods where id=?";
        try {
            pstml = connection.prepareStatement(sql);
            pstml.setString(1, goodsid);
            pstml.executeUpdate();
            connection.close();
            pstml.close();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    //修改发布物品的信息
    @Override
    public boolean UpdateGoods(Goods goods) {
        Connection connection = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        String sql = "update goods set name=?,imgurl=?,price=?,content=? where id=?";
        try {
            pstmt = connection.prepareStatement(sql);
            pstmt.setString(1, goods.getName());
            pstmt.setString(2, goods.getImgUrl());
            pstmt.setString(3, goods.getPrice());
            pstmt.setString(4, goods.getContent());
            pstmt.setString(5, goods.getId());
            pstmt.executeUpdate();
            pstmt.close();
            connection.close();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean UpdateTradingFinishGoods(String  goodsid) {
        Connection connection = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        String sql = "update goods set state=? where id=?";
        try {
            pstmt = connection.prepareStatement(sql);
            pstmt.setString(1, "false");
            pstmt.setString(2, goodsid);
            System.out.println("交易完成物品修改SQL语句:"+pstmt);
            pstmt.executeUpdate();
            pstmt.close();
            connection.close();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<Goods> ShowGoods() {
//        Date date = new Date();
        Connection con = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "select * from goods ORDER BY date DESC";

        List<Goods> list = new ArrayList<>();
        try {
            pstmt = con.prepareStatement(sql);
            System.out.println("显示所有物品SQL语句"+pstmt);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                Goods goods = new Goods();
                goods.setId(rs.getString("id"));
                goods.setUserId(rs.getString("userid"));
                goods.setName(rs.getString("name"));
                goods.setImgUrl(rs.getString("imgurl"));
                goods.setDate(rs.getString("date"));
                goods.setPrice(rs.getString("price"));
                goods.setContent(rs.getString("content"));
                goods.setState(rs.getString("state"));
                System.out.println("物品ID"+goods.getId());
                if (goods.getState().equals("true")){
                    list.add(goods);
                }
            }
            rs.close();
            pstmt.close();
            con.close();
            System.out.println("物品list长度"+list.size());
            return list;
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null,
                    "数据库访问失败");
            return null;
        }
    }

    //按照分类查询物品
    @Override
    public List<Goods> ShowGoods(String goods_category) {
//        Date date = new Date();
        Connection con = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "select * from goods where category=? ORDER BY date DESC";
        List<Goods> list = new ArrayList<>();
        try {
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, goods_category);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                Goods goods = new Goods();
                goods.setId(rs.getString("id"));
                goods.setName(rs.getString("name"));
                goods.setImgUrl(rs.getString("imgurl"));
                goods.setPrice(rs.getString("price"));
                goods.setState(rs.getString("state"));
                if (goods.getState().equals("true")){
                    list.add(goods);
                }
            }
            rs.close();
            pstmt.close();
            con.close();
            return list;
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null,
                    "数据库访问失败");
            return null;
        }
    }

    //按照搜索查询物品
    @Override
    public List<Goods> ShowSerachGoods(String serach) {
//        Date date = new Date();
        Connection con = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "select * from goods where name like ? ORDER BY date DESC";
        List<Goods> list = new ArrayList<>();
        try {
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, "%" + serach + "%");
            System.out.println(pstmt);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                Goods goods = new Goods();
                goods.setId(rs.getString("id"));
                goods.setName(rs.getString("name"));
                goods.setImgUrl(rs.getString("imgurl"));
                goods.setPrice(rs.getString("price"));
                goods.setState(rs.getString("state"));
                if (goods.getState().equals("true")){
                    list.add(goods);
                }
            }
            rs.close();
            pstmt.close();
            con.close();
            return list;
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null,
                    "数据库访问失败");
            return null;
        }
    }

    @Override
    public List<Goods> ShowUserGoods(String user_id) {
        Connection con = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "select * from goods where userid=? ORDER BY date DESC";
        List<Goods> list = new ArrayList<>();
        try {
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, user_id );
            System.out.println(pstmt);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                Goods goods = new Goods();
                goods.setId(rs.getString("id"));
                goods.setName(rs.getString("name"));
                goods.setImgUrl(rs.getString("imgurl"));
                goods.setDate(rs.getString("date"));
                goods.setState(rs.getString("state"));
                list.add(goods);
            }
            rs.close();
            pstmt.close();
            con.close();
            return list;
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null,
                    "数据库访问失败");
            return null;
        }
    }

    @Override
    public List<Goods> findByPageGoods(int page) {
        //        Date date = new Date();
        int begin = (page-1) * 5;
        Connection con = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "select * from goods order by date desc limit ?,5";
        List<Goods> list = new ArrayList<>();
        try {
            pstmt = con.prepareStatement(sql);
            pstmt.setInt(1, begin);
            System.out.println(pstmt);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                Goods goods = new Goods();
                goods.setId(rs.getString("id"));
                goods.setName(rs.getString("name"));
                goods.setImgUrl(rs.getString("imgurl"));
                goods.setPrice(rs.getString("price"));
                goods.setState(rs.getString("state"));
                if (goods.getState().equals("true")){
                    list.add(goods);
                }
            }
            rs.close();
            pstmt.close();
            con.close();
            return list;
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null,
                    "数据库访问失败goods部分");
            return null;
        }
    }

    @Override
    public int goodsCount() {
        //        Date date = new Date();
        Connection con = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        int count = 0;
        String sql = "select count(*) from goods";
        try {
            pstmt = con.prepareStatement(sql);
            System.out.println(pstmt);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                count = rs.getInt(1);
            }
            rs.close();
            pstmt.close();
            con.close();
            return count;
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null,
                    "数据库访问失败");
            return count;
        }
    }
    @Override
    public Goods ShowGoodsInfo(String goods_id) {
//        Date date = new Date();
        Connection con = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "select * from goods where id=?";
        try {
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, goods_id);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                Goods goods = new Goods();
                goods.setId(rs.getString("id"));
                goods.setUserId(rs.getString("userid"));
                goods.setName(rs.getString("name"));
                goods.setImgUrl(rs.getString("imgurl"));
                goods.setDate(rs.getString("date"));
                goods.setPrice(rs.getString("price"));
                goods.setContent(rs.getString("content"));
                goods.setState(rs.getString("state"));
                goods.setCategory(rs.getString("category"));
                return goods;
            }
            rs.close();
            pstmt.close();
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Goods GetSmallGoodsInfo(String goods_id) {
        Connection con = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "select * from goods where id=?";
        try {
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, goods_id);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                Goods goods = new Goods();
                goods.setName(rs.getString("name"));
                goods.setImgUrl(rs.getString("imgurl"));
                return goods;
            }
            rs.close();
            pstmt.close();
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String GetGoodsUserId(String goods_id) {
        Connection con = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "select * from goods where id=?";
        try {
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, goods_id);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                Goods goods = new Goods();
                goods.setUserId(rs.getString("userid"));
                return goods.getUserId();
            }
            rs.close();
            pstmt.close();
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
